-- Minimap Button Handling


local MinimapButton = CreateFrame('Button', 'AspireBidMinimapButton', Minimap)
AspireBid.Minimap = MinimapButton;
AspireBid.Minimap.dragging = false;


function AspireBid.Minimap:Load()

	self:SetFrameStrata('MEDIUM')
	self:SetWidth(31); self:SetHeight(31)
	self:SetFrameLevel(9)
	self:RegisterForClicks('anyUp')
	self:RegisterForDrag('LeftButton')
	self:SetHighlightTexture('Interface\\Minimap\\UI-Minimap-ZoomButton-Highlight')

	local overlay = self:CreateTexture(nil, 'OVERLAY')
	overlay:SetWidth(53); overlay:SetHeight(53)
	overlay:SetTexture('Interface\\Minimap\\MiniMap-TrackingBorder')
	overlay:SetPoint('TOPLEFT')

	local icon = self:CreateTexture(nil, 'BACKGROUND')
	icon:SetWidth(20); icon:SetHeight(20)
	icon:SetTexture('Interface\\AddOns\\AspireBid\\Aspire-Icon')
	icon:SetTexCoord(0.05, 0.95, 0.05, 0.95)
	icon:SetPoint('TOPLEFT', 7, -5)		
	self.icon = icon
	
	self:SetScript('OnEnter', self.OnEnter)
	self:SetScript('OnLeave', self.OnLeave)
	self:SetScript('OnClick', self.OnClick)
	self:SetScript('OnDragStart', self.OnDragStart)
	self:SetScript('OnDragStop', self.OnDragStop)
	self:SetScript('OnMouseDown', self.OnMouseDown)
	self:SetScript('OnMouseUp', self.OnMouseUp)
	
end

-- The Saved Variables are loaded, show minimap button
function AspireBid.Minimap:AddonLoaded()
	AspireBid.Minimap:UpdatePosition()
	
	if AspireBid.BaseGetSetting( "MinimapShown" ) then
		AspireBid.Minimap:Show()
	else
		AspireBid.Minimap:Hide()
	end
end

function AspireBid.Minimap:Toggle( )

	if AspireBid.BaseGetSetting( "MinimapShown" ) then
		AspireBid.BaseSetSetting( "MinimapShown", false )
		AspireBid.Minimap:Hide()
	else
		AspireBid.BaseSetSetting( "MinimapShown", true )
		AspireBid.Minimap:Show()
	end

end


function AspireBid.Minimap:OnClick( button )
	if button == 'LeftButton' then
		AspireBid.Window.Toggle();	
	elseif button == 'RightButton' then
		AspireBid.Config.Toggle();
	end
	self:OnEnter()
end


function AspireBid.Minimap:OnEnter()

	if not self.dragging then	
		GameTooltip:SetOwner(self, 'ANCHOR_TOPRIGHT')
		GameTooltip:AddLine( "AspireBid "..AspireBid.VERSION,1,1,1 );   
		GameTooltip:AddLine( "<Left Click> Toggle Aspire Bidding Window");				
		GameTooltip:AddLine( "<Right Click> Open Settings Window");
		GameTooltip:Show();		
	end
end

function AspireBid.Minimap:OnLeave()
	GameTooltip:Hide()
end

function AspireBid.Minimap:OnDragStart()
	self.dragging = true
	self:LockHighlight()
	self.icon:SetTexCoord(0, 1, 0, 1)
	self:SetScript('OnUpdate', self.OnUpdate)
	GameTooltip:Hide()
end

function AspireBid.Minimap:OnUpdate()

	local mx, my = Minimap:GetCenter()
	local px, py = GetCursorPosition()
	local scale = Minimap:GetEffectiveScale()

	px, py = px / scale, py / scale

	AspireBid.BaseSetSetting("MinimapPosition", math.deg(math.atan2(py - my, px - mx)) % 360)
	self:UpdatePosition()
end

function AspireBid.Minimap:OnDragStop()
	self.dragging = nil
	self:SetScript('OnUpdate', nil)
	self.icon:SetTexCoord(0.05, 0.95, 0.05, 0.95)
	self:UnlockHighlight()
end

function AspireBid.Minimap:OnMouseDown()
	self.icon:SetTexCoord(0, 1, 0, 1)
end

function AspireBid.Minimap:OnMouseUp()
	self.icon:SetTexCoord(0.05, 0.95, 0.05, 0.95)
end


--magic fubar code for updating the minimap button's position
--I suck at trig, so I'm not going to bother figuring it out
function MinimapButton:UpdatePosition()
	local angle = math.rad(AspireBid.BaseGetSetting("MinimapPosition") or random(0, 360))
	local cos = math.cos(angle)
	local sin = math.sin(angle)
	local minimapShape = GetMinimapShape and GetMinimapShape() or 'ROUND'

	local round = false
	if minimapShape == 'ROUND' then
		round = true
	elseif minimapShape == 'SQUARE' then
		round = false
	elseif minimapShape == 'CORNER-TOPRIGHT' then
		round = not(cos < 0 or sin < 0)
	elseif minimapShape == 'CORNER-TOPLEFT' then
		round = not(cos > 0 or sin < 0)
	elseif minimapShape == 'CORNER-BOTTOMRIGHT' then
		round = not(cos < 0 or sin > 0)
	elseif minimapShape == 'CORNER-BOTTOMLEFT' then
		round = not(cos > 0 or sin > 0)
	elseif minimapShape == 'SIDE-LEFT' then
		round = cos <= 0
	elseif minimapShape == 'SIDE-RIGHT' then
		round = cos >= 0
	elseif minimapShape == 'SIDE-TOP' then
		round = sin <= 0
	elseif minimapShape == 'SIDE-BOTTOM' then
		round = sin >= 0
	elseif minimapShape == 'TRICORNER-TOPRIGHT' then
		round = not(cos < 0 and sin > 0)
	elseif minimapShape == 'TRICORNER-TOPLEFT' then
		round = not(cos > 0 and sin > 0)
	elseif minimapShape == 'TRICORNER-BOTTOMRIGHT' then
		round = not(cos < 0 and sin < 0)
	elseif minimapShape == 'TRICORNER-BOTTOMLEFT' then
		round = not(cos > 0 and sin < 0)
	end

	local x, y
	if round then
		x = cos*80
		y = sin*80
	else
		x = math.max(-82, math.min(110*cos, 84))
		y = math.max(-86, math.min(110*sin, 82))
	end

	self:SetPoint('CENTER', x, y)
end



AspireBid.Minimap:Load();